﻿using Azylee.WinformSkin.APIUtils;
using JMBox;
using JMBox.Task;
using JustMetClient.Utils.View;
using static JMBox.Config.R;

namespace JustMetClient.Forms.View
{
    /// <summary>
    /// 悬浮窗程序
    /// </summary>
    public partial class TopForm : NoTitleFormBase
    {
        private Point _ptMouseCurrentPos, _ptMouseNewPos, _ptFormPos, _ptFormNewPos;
        //鼠标悬浮标志
        private bool _blnMouseDown = false;
        //主界面
        private MainForm _pParent;
        //增加菜单列表
        private ContextMenuStrip _docMenu;
        private RefreshTask refreshTask;


        public TopForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 从主界面跳转至悬浮窗调用的构造方法
        /// </summary>
        /// <param name="pMain"></param>
        public TopForm(MainForm pMain)
        {
            InitializeComponent();
            refreshTask = new RefreshTask();
            _pParent = pMain;
            Contact.TopUILabel = LB_Status;
            LB_Status.Text = ClearTask.SpaceCount + "";
            LB_Status.BackColor = ColorTranslator.FromHtml("#C90027");
        }

        /// <summary>
        /// 打开主界面
        /// </summary>
        private void SwitchToMain()
        {
            _pParent.RestoreWindow();
            this.Hide();
        }

        public void RefleshTipTitle(string tipTitle)
        {
            LB_Status.Text = tipTitle;
        }

        public void RefleshTipColor()
        {
            if(GlobalStatus.OpenClear == true)
            {
                LB_Status.BackColor = ColorTranslator.FromHtml("#13ABF1");
            }
            else
            {
                LB_Status.BackColor = ColorTranslator.FromHtml("#C90027");
            }
        }


        #region 上下文事件
        private void OpenLable_Click(object sender, System.EventArgs e)
        {
            SwitchToMain();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopForm_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.TopMost = true;
            this.Show();
            this.Top = 100;
            this.Left = Screen.PrimaryScreen.Bounds.Width - 200;
            refreshTask.Start();

            _docMenu = new ContextMenuStrip();
            ToolStripMenuItem OpenLable = new ToolStripMenuItem();
            OpenLable.Text = "打开主窗体";
            OpenLable.Click += new EventHandler(OpenLable_Click);
            ToolStripMenuItem ExitLable = new ToolStripMenuItem();
            ExitLable.Text = "退出程序";
            ExitLable.Click += new EventHandler(ExitLable_Click);

            _docMenu.Items.AddRange(new ToolStripMenuItem[] { OpenLable, ExitLable });
            this.ContextMenuStrip = _docMenu;
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (_blnMouseDown)
            {
                //Get the current postion of the mouse in the screen.
                _ptMouseNewPos = Control.MousePosition;

                //Set window postion.
                _ptFormNewPos.X = _ptMouseNewPos.X - _ptMouseCurrentPos.X + _ptFormPos.X;
                _ptFormNewPos.Y = _ptMouseNewPos.Y - _ptMouseCurrentPos.Y + _ptFormPos.Y;

                //Save window postion.
                Location = _ptFormNewPos;
                _ptFormPos = _ptFormNewPos;

                //Save mouse pontion.
                _ptMouseCurrentPos = _ptMouseNewPos;
            }
        }

        private void TopForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _blnMouseDown = true;

                //Save window postion and mouse postion.
                _ptMouseCurrentPos = Control.MousePosition;
                _ptFormPos = Location;
            }

        }
        private void TopForm_DoubleClick(object sender, EventArgs e)
        {
            SwitchToMain();
        }

        private void TopForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _blnMouseDown = false;
            }
        }

        private void ExitLable_Click(object sender, System.EventArgs e)
        {
            if(_pParent != null){
                _pParent.ExitApp();
            }
        }
        #endregion

        #region 鼠标点击事件


        private void LB_Status_Click(object sender, EventArgs e)
        {
            SwitchToMain();
        }

        private void TP_Status_Popup(object sender, PopupEventArgs e)
        {

        }

        private void TopForm_MouseHover(object sender, EventArgs e)
        {

        }

        private void LB_Status_DoubleClick(object sender, EventArgs e)
        {
            SwitchToMain();
        }

        private void LB_Status_MouseMove(object sender, MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                FormStyleAPI.ReleaseCapture();
                FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
            }
        }

        private void LB_Status_MouseHover(object sender, EventArgs e)
        {
            TooltipTool.ShowTooltip(LB_Status, TP_Title, "运行中", 1000);
        }

        private void TopForm_MouseEnter(object sender, EventArgs e)
        {
            this.TP_Status.Active = true;
            this.TP_Status.AutoPopDelay = 500000;
            this.TP_Status.InitialDelay = 500;
            this.TP_Status.ReshowDelay = 100;
            this.TP_Status.ShowAlways = true;
        }

        private void TopForm_MouseLeave(object sender, EventArgs e)
        {
            this.TP_Status.Active = false;
        }

        #endregion
    }
}
